﻿using System;
using System.Collections.Generic;

namespace WmsApp.UI
{
    public partial class QInStoreBillDtl : QInStoreBill, IQueryControl
    {
        public QInStoreBillDtl()
        {
            InitializeComponent();
            this.numericScopeQuerier1.Field = "AmountTax - isnull(InvoiceDtlTotalAmountTax,0)";
            var list = new List<ICheckQuerierItem>
                           {
                               CheckQuerierItemFactory.Create("发票已确认", this.checkQuerier1),
                               CheckQuerierItemFactory.Create("发票未确认", this.checkQuerier1)
                           };
            checkQuerier1.BindingCheckQuerierItem(list);
        }

        #region IQueryControl Members

        public new string GetCondition()
        {
            string condition = "";
            if (lookUpEditBillState.EditValue != null && !String.IsNullOrEmpty(lookUpEditBillState.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBill where BillState ='{0}')", lookUpEditBillState.EditValue));
            }
            if (lookUpEditGoodsCodeType.EditValue != null && !String.IsNullOrEmpty(lookUpEditGoodsCodeType.EditValue.ToString()))
            {
                condition += (String.Format(" and  NewGoodsCode ='{0}' ", lookUpEditGoodsCodeType.EditValue));
            }
            if (lookUpEditInStoreType.EditValue != null && !String.IsNullOrEmpty(lookUpEditInStoreType.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBill where ExtVarChar1 ='{0}') ", lookUpEditInStoreType.EditValue));
            }
            if (lookUpEditProvider.EditValue != null && !String.IsNullOrEmpty(lookUpEditProvider.EditValue.ToString()))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBill  where ProviderCode ='{0}')", lookUpEditProvider.EditValue));
            }
            if (lookUpEditStoreType.EditValue != null && !String.IsNullOrEmpty(lookUpEditStoreType.EditValue.ToString()))
            {
                condition += (String.Format(" and StoreTypeCode ='{0}' ", lookUpEditStoreType.EditValue));
            }
            if (lookUpEditUnit.EditValue != null && !String.IsNullOrEmpty(lookUpEditUnit.EditValue.ToString()))
            {
                condition += (String.Format(" and  UnitCode ='{0}' ", lookUpEditUnit.EditValue));
            }

            if (!String.IsNullOrEmpty(this.textEditInStoreBIllID.Text))
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBill  where BillID  like '%{0}%' ) ", textEditInStoreBIllID.Text));
            }

            if (this.dateEditBillDateFrom.EditValue != null)
            {
                condition += (String.Format(" and BillID in ( select BillID from dbo.InStoreBill  where InBillDate  >= '{0}' )", dateEditBillDateFrom.Text));
            }

            if (this.dateEditBillDateTo.EditValue != null)
            {
                condition += (String.Format("and BillID in ( select BillID from dbo.InStoreBill  where  InBillDate  <= '{0}')", dateEditBillDateTo.Text));
            }

            if (!String.IsNullOrEmpty(this.customCondition))
            {
                condition += customCondition;
            }

            if (! String.IsNullOrEmpty(this.textEditInvoiceNo.Text.Trim()))
            {
                condition += (String.Format(" and  InvoiceNoList  like '%{0}%' ", this.textEditInvoiceNo.Text.Trim()));
            }

            //项目号，请购单号，合同号
            if (!String.IsNullOrEmpty(this.textEditExtVarChar1.Text.Trim()))
            {
                condition += (String.Format(" and  ExtVarChar1  like '%{0}%' ", this.textEditExtVarChar1.Text.Trim()));
            }

            if (!String.IsNullOrEmpty(this.textEditExtVarChar2.Text.Trim()))
            {
                condition += (String.Format(" and  ExtVarChar2  like '%{0}%' ", this.textEditExtVarChar2.Text.Trim()));
            }

            if (!String.IsNullOrEmpty(this.textEditContractDtlNo.Text.Trim()))
            {
                condition += (String.Format(" and  ContractDtlNo  like '%{0}%' ", this.textEditContractDtlNo.Text.Trim()));
            }

            //新物料名称
            if (!String.IsNullOrEmpty(this.textEditNewGoodsName.Text.Trim()))
            {
                condition += (String.Format(" and  NewGoodsName  like '%{0}%' ", this.textEditNewGoodsName.Text.Trim()));
            }

            //库位
            if (!String.IsNullOrEmpty(this.textEditStoreLocation.Text.Trim()))
            {
                condition += (String.Format(" and  StoreLocation  like '%{0}%' ", this.textEditStoreLocation.Text.Trim()));
            }

            string query = this.checkQuerier1.GetQueryCondition();
            if (!String.IsNullOrEmpty(query))
            {
                condition += ("and BillID in ( select BillID from dbo.InStoreBill  where  ConfirmInvoiceFlag in " + query + " )");
            }

            string query2 = this.numericScopeQuerier1.GetQueryCondition();
            if (!String.IsNullOrEmpty(query2))
            {
                condition += query2;
            }

            return condition;
        }

        #endregion
    }
}
